﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIProxy;
using PPIDesktopClient.Entities;
using System.Data;
using VMind.Global;
using PPIDesktopClient.General;


namespace PPIDesktopClient.Logic
{
    public class LUtility_Asset_XMissionStructure_Image
    {
        #region Query
        public static List<EUtility_Asset_XMissionStructures_Image> GetUtility(int structureID)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            DataTable _dt = PPIProxy.WUtility_Asset_XMissionStructure_Image.SelectAllByUtility(structureID,reader.GetConnectionString()).Tables[0];
            return ConvertTo(_dt);
        }

        public static List<EUtility_Asset_XMissionStructures_Image> GetAll(int offset)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            DataTable _dt =
                PPIProxy.WVendor_Visit_XMissionStructures_Image.GetServerImages(offset, reader.GetConnectionString()).
                    Tables[0];
            return ConvertTo(_dt);
        }

        private static List<EUtility_Asset_XMissionStructures_Image> ConvertTo(DataTable dt)
        {
            List<EUtility_Asset_XMissionStructures_Image> _ImageList = new List<EUtility_Asset_XMissionStructures_Image>();
            EUtility_Asset_XMissionStructures_Image _Image;
            foreach (DataRow  _ImageRow in dt.Rows)
            {
                _Image = new EUtility_Asset_XMissionStructures_Image();
                _Image.Id = ConvertHelper.ToInt32(_ImageRow["Vendor_Visit_XMissionStructures_ImageId"]);
                _Image.Direction = ConvertHelper.Todouble(_ImageRow["Direction"]);
                _Image.Latitude = ConvertHelper.Todouble(_ImageRow["Latitude"]);
                _Image.Longitude = ConvertHelper.Todouble(_ImageRow["Longitude"]);
                _Image.Altitude = ConvertHelper.Todouble(_ImageRow["Altitude"]);
                _Image.SatelliteCount = ConvertHelper.Toint(_ImageRow["SatelliteCount"]);
                _Image.SatelliteTime = ConvertHelper.ToDateTime(_ImageRow["SatelliteTime"]);
                _Image.SolutionAvailable = ConvertHelper.Tobool(_ImageRow["SolutionAvailable"]);
                _Image.FileName = ConvertHelper.Tostring(_ImageRow["Filename"]);
                _Image.Asset = new EUtility_Asset_XMissionStructure();
                _Image.Asset.Id = ConvertHelper.ToInt32(_ImageRow["Vendor_Visit_XMissionStructureFK"]);
                _Image.File = EUtility_Asset_XMissionStructures_Image.GetImageFile(_Image.Asset.Id, _Image.FileName);
                _Image.Pole = new EUtility_Asset_XMissionStructures_Pole(ConvertHelper.ToInt32(_ImageRow["Vendor_Visit_XMissionStructures_PoleFK"]));
                _ImageList.Add(_Image);
            }
            return _ImageList;
        }

        public static List<EUtility_Asset_XMissionStructures_Image> GetByPole(int poleId, List<EUtility_Asset_XMissionStructures_Image> imageList)
        {
            List<EUtility_Asset_XMissionStructures_Image> _newList = new List<EUtility_Asset_XMissionStructures_Image>();

            foreach (EUtility_Asset_XMissionStructures_Image i in imageList)
            {
                if (i.Pole.Id == poleId)
                {
                    _newList.Add(i);
                }
            }
            return _newList;

        }

        public static List<EUtility_Asset_XMissionStructures_Image> GetByStructureID(int structureID, List<EUtility_Asset_XMissionStructures_Image> imageList)
        {
            EUtility_Asset_XMissionStructures_Image _Image;
            List<EUtility_Asset_XMissionStructures_Image> _Images = new List<EUtility_Asset_XMissionStructures_Image>();
            foreach (EUtility_Asset_XMissionStructures_Image i in imageList)
            {
                if (i.Asset.Id == structureID)
                    _Images.Add(i);
            }
            return _Images;
        }

        public static List<EUtility_Asset_XMissionStructures_Image> GetByPoleID(int poleId, List<EUtility_Asset_XMissionStructures_Image> imageList)
        {
            EUtility_Asset_XMissionStructures_Image _Image;
            List<EUtility_Asset_XMissionStructures_Image> _Images = new List<EUtility_Asset_XMissionStructures_Image>();
            foreach (EUtility_Asset_XMissionStructures_Image i in imageList)
            {
                if (i.Pole.Id == poleId)
                    _Images.Add(i);
            }
            return _Images;
        }

        #endregion


        #region CRUD
        public static void Disassociate(int imageId)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            WUtility_Asset_XMissionStructure_Image.Disassociate(imageId,_Reader.GetConnectionString());
        }

        public static void Associate(int imageid, int poleId)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            WUtility_Asset_XMissionStructure_Image.Associate(imageid, poleId,_Reader.GetConnectionString());
        }

        public static int Insert(EUtility_Asset_XMissionStructures_Image image)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            return WUtility_Asset_XMissionStructure_Image.Insert(image.Asset.Id, image.SolutionAvailable, image.File.Name , EBaseImage.ToBytes(image.File.FullName), image.Latitude, image.Altitude, image.Longitude, image.SatelliteTime, image.SatelliteCount, image.Direction, image.Pole.Id,_Reader.GetConnectionString());
        }

        public static void Delete(Int32 imageID)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            WUtility_Asset_XMissionStructure_Image.Delete(imageID,_Reader.GetConnectionString());
        }

        #endregion
    }
}
